;;;
;;;
;;;	Author:
;;;		Henry C. Francis
;;;		425 N. Ashe St.
;;;		Southern Pines, NC 28387
;;;
;;;	http://www.paracadd.com
;;;	All rights reserved.
;;;
(defun c:uplan ()
  (if(and(>(getvar"cvport")1)(eq(getvar"tilemode")0))
    nil
    (if (eq(getvar"tilemode")0)
      (vla-put-MSpace (vla-get-Activedocument (vlax-get-Acad-Object)) :vlax-true)
;;;      (command "._mspace")
      (princ "\nPaper space is not active")
    )
  )
  (setq rtview (ukword 1 "continue" "Make the plan view current before continuing." "continue"))
  (setq curvno (getvar"cvport"))
;;;  (regapp "ACAD")
  (setq cvpss (ssget "X" (LIST (CONS 410 (GETVAR "CTAB"))(CONS 69 curvno))))
  (setq cvpent (entget (ssname cvpss 0)(list "ACAD")))
  (setq vpscl (/(getvar"viewsize")(cdr(assoc 41 cvpent))))
  (cond
    ((or(eq(getvar"lunits")1)(eq(getvar"lunits")5))
      (princ (strcat "Viewport scale is: 1:" (rtos vpscl 2 4) " (1:"(rtos vpscl 2 4)")"))
      )
    ((eq(getvar"lunits")2)
      (princ (strcat "Viewport scale is: 1\"=" (rtos vpscl 2 4)"' (1:"(rtos vpscl 2 4)")"))
      )
    ((eq(getvar"lunits")3)
      (princ (strcat "Viewport scale is: 1\"=" (rtos (/ vpscl 12) 2 4)"' (1:"(rtos vpscl 2 4)")"))
      )
    ((eq(getvar"lunits")4)
      (princ (strcat "Viewport scale is: " (rtos (/ 12.0000 vpscl)5 4)"\"=1'-0\" (1:"(rtos vpscl 2 4)")"))
      )
  )
  (setq cvcntr (getvar"viewctr")
        cvsize (getvar"viewsize")
        cvtwst (getvar"viewtwist")
        cvas40 (cdr(assoc 40 cvpent))
        cvas41 (cdr(assoc 41 cvpent))
        cvscle (/ cvsize cvas41)
        cvwdth (* cvas40 cvscle)
        cvpllx (-(car cvcntr)(/ cvwdth 2))
        cvplly (/(-(cadr cvcntr)(/ cvsize 2))10)
  )
;  (princ (strcat "\nCurrent plan beginning station (in hundreds) = " (itoa(atoi(rtos(/ cvpllx 100))))))
  (setq firsta (uint 1 "" "    New plan beginning station" firsta))
  (setq lasta (uint 1 "" "    New plan ending station" lasta))
;  (princ (strcat "\nCurrent alignment offset from view centerline = " (itoa(atoi(rtos cvplly)))))
  (setq fircnt 1)
  (while (< (nth 1(nth fircnt align_lst)) firsta)
    (setq fircnt (1+ fircnt)
          lascnt fircnt)
  )
  (while (and(< (nth 1(nth lascnt align_lst)) lasta)(< lascnt (1- (length align_lst))))
    (setq lascnt (1+ lascnt))
  )
  (setq fsta_pt (polar
                  (nth 9(nth fircnt align_lst))
                  (angle
                    (nth 9(nth fircnt align_lst))
                    (nth 9(nth (1- fircnt) align_lst))
                  )
                  (-(nth 1(nth fircnt align_lst))firsta)
                )
  )
  (setq lsta_pt (polar
                  (nth 9(nth (1- lascnt) align_lst))
                  (angle
                    (nth 9(nth (1- lascnt) align_lst))
                    (nth 9(nth lascnt align_lst))
                  )
                  (- lasta (nth 1(nth (1- lascnt) align_lst)))
                )
  )
  (setq cvtwok (ukword 1 "Yes No" "Is current viewtwist ok?" (if cvtwok cvtwok "Yes")))
  (if (eq cvtwok "No")
    (progn
      (setq corrwid (uint 1 "" "    New alignment corridor width" corrwid))
    )
    (progn
      (if(>(abs(-(* pi 2) cvtwst (angle cvcntr lsta_pt)))pi)
        (setq rangdif (-(* 2 pi)(abs(-(* pi 2) cvtwst (angle cvcntr lsta_pt)))))
        (setq rangdif (abs(-(* pi 2) cvtwst (angle cvcntr lsta_pt))))
      )
      (if(>(abs(-(* pi 2) cvtwst (angle fsta_pt cvcntr)))pi)
        (setq langdif (-(* 2 pi)(abs(-(* pi 2) cvtwst (angle fsta_pt cvcntr)))))
        (setq langdif (abs(-(* pi 2) cvtwst (angle fsta_pt cvcntr))))
      )
;      (setq rolpan (ukword 1 "Right Left" "Pan fit <R>ight or <L>eft end?" (if rolpan rolpan "Right")))
      (if ;(eq rolpan "Right")
         (equal (angle
                  (nth 9(nth (1- fircnt) align_lst))
                  (nth 9(nth fircnt align_lst))
                )
                (-(* pi 2)cvtwst)
                0.001
         )
         (setq pandist (*(-(/(*(/ cvas40 cvas41)cvsize)2.000)
                         (*(distance cvcntr fsta_pt)(cos langdif))0.1)-1)
         )
;        (equal (angle
;                 (nth 9(nth (1- lascnt) align_lst))
;                 (nth 9(nth lascnt align_lst))
;               )
;               (-(* pi 2)cvtwst)
;               0.001
;        )
        (setq pandist (-(/(*(/ cvas40 cvas41)cvsize)2.000)
                        (*(distance cvcntr lsta_pt)(cos langdif))0.1)
        )
      )
      (if (<(abs pandist)0.05)
        (princ "\nCurrent plan orientation is OK! ")
        (progn
          (setq to_pt (polar cvcntr (-(* pi 2)cvtwst) pandist))
          (command ".pan" cvcntr to_pt)
          (princ (strcat "\nPlan panned " (rtos pandist 2 2) "'"))
        )
      )
    )
  )
  (princ)
);defun
(defun panprv ()
  (progn
    (setq nvctrx (+(* cursta 100)(/ cvwdth 2))
          nvctry (+(* cedatum 10)(/ cvsize 2))
          pxdist (-(car cvcntr)nvctrx)
          pydist (-(cadr cvcntr)nvctry)
    )
    (princ (strcat "dX=" (rtos pxdist) ", dY=" (rtos pydist)))                  ;"\nX=" (rtos nvctrx) ", Y=" (rtos nvctry)
    (command "._zoom" "c" (strcat (rtos nvctrx)","(rtos nvctry))"")
  )
)